package com.lkd.emq.service.impl;

import com.lkd.emq.handler.MsgHandler;
import com.lkd.emq.handler.MsgHandlerContext;
import com.lkd.emq.service.MqttService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 消息分发处理器
 */
@Component
@Slf4j
public class MqttServiceImpl implements MqttService {

    @Autowired
    private MsgHandlerContext msgHandlerContext;

    /**
     * mqtt消息处理
     *
     * @param topic
     * @param message
     */
    @Override
    public void processMessage(String topic, MqttMessage message) throws IOException {
        String msgContent = new String(message.getPayload());
        //执行业务。。。开始
        MsgHandler msgHandler = msgHandlerContext.getMsgHandler(topic);
        if (msgHandler == null) {
            return;
        }
        msgHandler.process(msgContent);

        //执行业务。。。结束
    }
}